
var jName = getCookie('Jname');
//我的权限数组
var arrayTitle = new Array; 
var id='';
var currentPage = 1;
loadMyEssay(id);
findMyCatalogue();
function loadMyEssay(id) {
	$(document).ready(function() {
//		findMyCatalogue()
		init(currentPage);
	});
	function init(pageNo) {
		$("#thislist").html("");
		$.ajax({
			url: urlcore + "/api/product/selectProductList?current="+ pageNo,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {

				if(data.success == true) {
					$.each(data.data.records, function(i, n) {
						var id = n.id;
						var pic_url = n.picUrl;
						var imgStr1 = '';
						if(pic_url != null && pic_url != '') {
						var imgarr = pic_url.split("***");
						$.each(imgarr, function(index, value) {
					 	imgStr1="<img style=\"padding: 5px 5px 5px 5px;\" width=\"45px\" height=\"45px\" src=" + urlcore+value + ">" 
							});
						}						
						var classify_name = n.classify.name;
						var product_name = n.productName;
						var product_detail = n.productDetail;
						var init_price = n.initPrice;
						var final_price = n.finalPrice;
						var gmt_datetime = n.gmtDatetime;
						var status="";
						if(n.status=='1'){
							status="上架中";
						}else if(n.status=='3'){
							status="删除";
						}else if(n.status=='2'){
							status="已下架";
						}else{
							status="未知";
						}
						var thislist =
							'<tr class="footable-even" style="display: table-row;">' +
							'<td class="footable-visible">' + id + '</td>' +
							'<td class="footable-visible">' + classify_name + '</td>' +
							'<td class="footable-visible">' + product_name + '</td>' +
							'<td class="footable-visible">' + product_detail + '</td>' +
							'<td class="footable-visible">' + imgStr1 + '</td>' +
							'<td class="footable-visible">' + init_price + '</td>' +
							'<td class="footable-visible">' + final_price + '</td>' +
							'<td class="footable-visible">' + gmt_datetime + '</td>' +
							'<td class="footable-visible">' + status + '</td>' +
							'<td class="footable-visible footable-last-column">' +  
								'<a  hidden="hidden" class="" name="修改" href="product_update.html?id=' + id + '"> 修改</a>&nbsp;&nbsp;'+
								'<a  hidden="hidden" class="" name="删除" href="javascript:;" onclick="deleteeach(' + n.id + ')"> 删除</a>' + '</td>' +'</tr>';
						$('#thislist').append(thislist);
						$('#thiscount').html(data.data.records.total);
					});
					$("#pager").pager({
	                    pagenumber: pageNo,
	                    pagecount: data.data.pages,
	                    totalcount: data.data.total,
	                    buttonClickCallback: PageClick
	                });
					$.each(arrayTitle, function(i,k) {
						$('a[name="'+k+'"]').attr("hidden",false).attr("class","btn btn-primary btn-xs");
						$('a[data="'+k+'"]').attr("class","btn btn-sm btn-primary");
					});
				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;

					if(thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href = "login.html";
					}

				} else {
					if(data.msg != '空数据') {
						alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}
			},
			error: function() {
				alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}
function deleteeach(id){
		if(confirm("您确定要删除这条广告吗？")){
		$.ajax({
			url: urlcore + "/api/product/deleteOne?id="+id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
			     window.location="product_list.html"
			},
			error:function() {
				/* Act on the event */
				alert("error");
			}
		});	
	}
}

function findMyCatalogue(){
	$.ajax({
		url: urlcore + "/api/roleThirdCatalogue/findAllByUser?secondTitle="+jName,
		type: "GET",
		dataType: 'json',
		async: false,
		contentType: "application/json;charset=utf-8",
		success:function(data){
		if (data.success == true) {
			$.each(data.data, function(i,n) {
				arrayTitle.push(n.thirdCatalogue.title);
			});
		} else {
			alert(data.msg);
		}

		},
		error:function() {
			alert("error");
		}
	});
}

//function select1(){
//	$.ajax({
//		url: urlcore + "/api/product/selectProductList?current="+1,
//		type: "GET",
//		dataType: 'json',
//		async: false,
//		contentType: "application/json;charset=utf-8",
//		success:function(data){
//		if (data.success == true) {
//			$.each(data.data, function(i,n) {
//				arrayTitle.push(n.thirdCatalogue.title);
//			});
//		} else {
//			alert(data.msg);
//		}
//
//		},
//		error:function() {
//			alert("error");
//		}
//	});
//}
